package cn.itcast.controller;

import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    /**
     * 查询展示套餐列表
     * 请求方式: Get
     * 请求路径: /setmeal/findAll
     * 返回值: Setmeal集合
     * @return
     */
    @GetMapping("/findAll")
    public Result setmealFindAll(){
        List<Setmeal> list = setmealService.findAll();
        return new Result(true,"套餐列表查询成功",list);
    }

    /**
     * 根据id查询套餐详情
     * 请求方式: get
     * 请求路径: /setmeal/id
     */
    @GetMapping("/{id}")
    public Result findByIds(@PathVariable Integer id){
        Setmeal setmeal = setmealService.findById(id);
        //根据id查询出所有检查组
        List<CheckGroup> groupList = setmealService.findCheckgroupById(id);
        //根据检查组id查询出所有检查项
        for (CheckGroup checkGroup : groupList) {
            List<CheckItem> checkItemList =  setmealService.findCheckitem(checkGroup.getId());
            //把所有检查项集合设置为checkGroup属性
            checkGroup.setCheckItems(checkItemList);
        }
        //把检查组检查项设置为Setmeal属性 封装返回
        setmeal.setCheckGroups(groupList);
        return new Result(true,"根据ID查询成功",setmeal);

    }


}
